package org.oschina.api.user.bar.service;

import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.oschina.Config;
import org.oschina.Constants;
import org.oschina.Message;
import org.oschina.Module;
import org.oschina.ResultUtil;
import org.oschina.api.BaseService;
import org.oschina.api.manage.config.service.ConfigService;
import org.oschina.api.user.bar.dao.UserBarDAO;
import org.oschina.api.user.bar.model.UserBar;
import org.oschina.api.user.role.vo.UserBarVo;
import org.oschina.api.user.user.model.User;
import org.oschina.api.user.user.service.UserService;
import org.oschina.api.user.user.vo.UserVo;
import org.oschina.bean.Result;
import org.oschina.api.index.service.IndexService;
import org.oschina.security.annotation.Require;
import org.oschina.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserBarService extends BaseService {

	@Autowired
	private UserBarDAO barDAO;
	@Autowired
	private UserBarRoleService barRoleService;
	@Autowired
	private UserService userService;
	@Autowired
	private IndexService indexService;
	@Autowired
	private ConfigService configService;
	
	/**
	 * 返回
	 * @return
	 */
	@Require(authorization=Module.MODULE_9)
	public List<UserBarVo> findVo()
	{
		return barDAO.findVo();
	}
	
	/**
	 * 根据角色返回
	 * @param role
	 * @return
	 */
	@Require(authorization=Module.MODULE_9)
	public List<UserBarVo> findVoByRole(int role)
	{
		return _findVoByRole(role);
	}
	
	/**
	 * 更新
	 * @param bar
	 * @return
	 */
	@Require(authorization=Module.MODULE_9)
	public Result update(UserBar bar)
	{
		barDAO.update(bar);
		return ResultUtil.getSuccess(bar);
	}
	
	/**
	 * 创建
	 * @param bar
	 * @return
	 */
	@Require(authorization=Module.MODULE_9)
	public Result create(UserBar bar)
	{
		bar.setId(_getIndex());
		bar.setCreate_time(DateUtil.now());
		barDAO.create(bar);
		return ResultUtil.getSuccess(bar);
	}
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@Require(authorization=Module.MODULE_9)
	public Result delete(int id)
	{
		if(barDAO.delete(id)==0)
			return ResultUtil.getFailure(Message.ERROR_DATA_ERROR);
		
		barRoleService._removeByBar(id);
		return ResultUtil.getSuccess(Message.DELETE_SUCCESS);
	}
	
	/**
	 * 根据用户返回
	 * @return
	 */
	public List<UserBarVo> findVoByUser()
	{
		User user=userService.getAuthenticationInfo(getToken());
		UserVo vo=userService._toVo(user);
		if(user==null)
			return findVoByRole((Integer)configService.getValueById(Config.CONFIG_6, Integer.class));

		List<UserBarVo> result=findVoByRole(user.getRole());
		String oldStr[]=new String[]{"{username}"};
		String newStr[]=new String[]{vo.getUsername()};
		for(UserBarVo barVo:result)
		{
			barVo.setText(StringUtils.replaceEach(barVo.getText(),oldStr, newStr));
		}
		
		return result;
	}
	
	/**
	 * 根据ID返回
	 * @param id
	 * @return
	 */
	public UserBarVo _findVoById(int id)
	{
		return barDAO.findVoById(id);
	}
	
	/**
	 * 根据权限返回
	 * @param role
	 * @return
	 */
	private List<UserBarVo> _findVoByRole(int role)
	{
		return barDAO.findVoByRole(role);
	}
	
	private int _getIndex()
	{
		return indexService.getMajorById(Constants.INDEX_USER_BAR);
	}
}